import axios from "axios";
import {getJwt, removeJwt} from "@/api/jwt.js";

let api = axios.create({
    baseURL:"/api",
    timeout:3000
});

//配置一个响应拦截器
api.interceptors.response.use(resp=>{
    return resp.data;
},resp=>{
    console.log(resp);
    let data = resp.response.data;//后台相应的数据
    if (resp.status === 401){
        //现在的token无效，需要移除
        removeJwt();
        location.href = "/login";
    }

    return Promise.reject(resp.data.msg|"后台服务异常");
});

//配置请求拦截器
api.interceptors.request.use(configs=>{
    //配置请求头,再请求头中带一个jwt
    configs.headers["Authorization"] = getJwt();
    return configs;
},error => {
    return Promise.reject(error)
})

export default api;